﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NWN.GDY.Service.Core;
using NWN.GDY.Service.Entity;
using Sdo.Payment.Common;
using NWN.GDY.Service.Common;

namespace NWN.GDY.Web
{
    public partial class ajaxroom : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string result = "";
            string contenttype = "";
            try
            {
                string roomguid = Request["roomguid"];
                switch (Request["opName"])
                {
                    #region GetRoom
                    case "GetRoom":
                        string vplayrecord = string.IsNullOrEmpty(Request["vplayrecord"]) ? null : Request["vplayrecord"];
                        string vroom = string.IsNullOrEmpty(Request["vroom"]) ? null : Request["vroom"];
                        string vround = string.IsNullOrEmpty(Request["vround"]) ? null : Request["vround"];
                        string vucoin = string.IsNullOrEmpty(Request["vucoin"]) ? null : Request["vucoin"];
                        string vuroom = string.IsNullOrEmpty(Request["vuroom"]) ? null : Request["vuroom"];
                        string vuinfo = string.IsNullOrEmpty(Request["vuinfo"]) ? null : Request["vuinfo"];
                        string vchat = string.IsNullOrEmpty(Request["vchat"]) ? null : Request["vchat"];
                        string vadminroom = string.IsNullOrEmpty(Request["vadminroom"]) ? null : Request["vadminroom"];
                        string vroundresult = string.IsNullOrEmpty(Request["vroundresult"]) ? null : Request["vroundresult"];
                        long prid = long.Parse(Request["prid"]);
                        long chatid = long.Parse(Request["chatid"]);
                        result = XmlConfigMgr.ToXml(new GDYEngine().GetRoom(roomguid, CurrentUser, prid, vplayrecord, vroom, vround, vucoin, vuroom, vuinfo, chatid, vchat, vadminroom, vroundresult));                        
                        //result = XmlConfigMgr.ToXml(new GDYEngine().GetRoom(roomguid, CurrentUser, 0, null, null, null, null, null, null, 0, null, null));
                        Log.WriteLine(result);
                        contenttype = "text/xml";
                        break;
                    #endregion
                    #region Sit
                    case "Sit":
                        string seatidsit = Request["seatid"];
                        result = ToJSON(new GDYEngine().Sit(CurrentUser, roomguid, int.Parse(seatidsit)));
                        break;
                    #endregion
                    #region Stand
                    case "Stand":
                        result = ToJSON(new GDYEngine().Stand(CurrentUser, roomguid));
                        break;
                    #endregion
                    #region Ready
                    case "Ready":
                        result = ToJSON(new GDYEngine().Ready(CurrentUser, roomguid));
                        break;
                    #endregion
                    #region Discard
                    case "Discard":
                        string CardID = Request["cardids"]==null ? null : Request["cardids"];
                        string ArrageCardID = Request["ArrageCardID"]==null ? null : Request["ArrageCardID"];
                        string RoundGUID = Request["RoundGUID"]==null ? null : Request["RoundGUID"];
                        string lasttype = Request["lasttype"]==null ? "0" : Request["lasttype"];
                        if (string.IsNullOrEmpty(lasttype))
                            lasttype = "0";
                        string lastcardids = Request["lastcardids"]==null ? null : Request["lastcardids"];
                        result = ToJSON(new GDYEngine().Discard(CurrentUser, CardID, ArrageCardID, roomguid, RoundGUID, lastcardids, int.Parse(lasttype)));
                        break;
                    #endregion
                    #region Pass
                    case "Pass":
                        string RoundGUIDPass = Request["RoundGUID"];
                        result = ToJSON(new GDYEngine().Pass(CurrentUser, roomguid, RoundGUIDPass));
                        break;
                    #endregion
                    #region chat
                    case "Chat":
                        string ChatContent = Request["ChatContent"];
                        result = ToJSON(new GDYEngine().Chat(CurrentUser, roomguid, ChatContent));
                        break;
                    #endregion
                    default:
                        result = ToJSON((GDYConsts.ReturnDefine.AjaxException));
                        break;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                result = ToJSON((GDYConsts.ReturnDefine.AjaxException));
            }
            Response.Clear();
            if (!string.IsNullOrEmpty(contenttype))
                Response.ContentType = contenttype;
            Response.Write(result);
            Response.End();
        }
    }
}
